package com.xianggu.spring.aop.strategy;

import com.xianggu.spring.aop.config.WildcardMatcher;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author chengjy
 * @date 2025/3/10
 */
public class ExecutionPointStrategy implements PointStrategy {

    private String path;
    public ExecutionPointStrategy(String path) {
        this.path = path;
    }

    @Override
    public boolean match(Class targetClass, List<String> noProxyMethods) {
        boolean flag = false;
        // 根据类和方法的全限定名进行匹配
        for (Method declaredMethod : targetClass.getDeclaredMethods()) {
            String methodPath = targetClass.getName() + "." + declaredMethod.getName();

            if (WildcardMatcher.matches(methodPath, path)) {
                flag = true;
            }else{
                // 加入集合
                noProxyMethods.add(declaredMethod.getName());
            }
        }
        if (WildcardMatcher.matches(targetClass.getName(), path)) {
            noProxyMethods.removeAll(noProxyMethods);
            return true;
        }
        return flag;
    }

    @Override
    public String toString() {
        return "ExecutionPointStrategy{" +
                "path='" + path + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ExecutionPointStrategy that)) return false;

        return o.toString().equals(this.toString());
    }

    @Override
    public int hashCode() {
        return path != null ? path.hashCode() : 0;
    }
}
